# From http://davidfischer.name/2010/02/django-scripting-and-the-crontab-2/

import os, sys
# need to change this on installation.
# Just for debugging.
# Ugly hack
#sys.path.append('/home/bms/sign-generator-openscad/')
#sys.path.append('/home/bms/sign-generator-openscad/b9c')

from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from b9c.sign_generator import stl_sign_generator
from django.template.defaultfilters import slugify
from django.conf import settings

from sign_generator.models import *

# Class MUST be named 'Command'
class Command(BaseCommand):

    # Displayed from 'manage.py help mycommand'
    help = "Your help message"

    # make_option requires options in optparse format
    option_list = BaseCommand.option_list  + (
                        make_option('--myoption', action='store',
                            dest='myoption',
                            default='default',
                            help='Option help message'),
                  )
    fonts = Font.objects.order_by('font')
    font_template = settings.TEMPLATE_DIRS[0] + "/fonts/fonts.html"
    font_template_html =""
    font_text = "Grumpy wizards make toxic brew for the evil Queen and Jack."
    for font in fonts:
        print "Generating :", font
        font_fn = slugify(font) + ".png"
#        print settings.FONT_DIR  + font_fn
        stl_sign_generator.generatePNGFile(settings.FONT_DIR + font_fn, font_text, font, width=550)
        font_template_html = font_template_html + "<p>" + str(font) + " <br/><img src=/" + settings.FONT_URL + font_fn + " alt=\"" + str(font) + " - " + font_text + "\"  /></p>\n"
    f=open(font_template, 'w')
    f.write(font_template_html)
    f.close()


    def handle(self, *app_labels, **options):
        """
        app_labels - app labels (eg. myapp in "manage.py reset myapp")
        options - configurable command line options
        """

        # Return a success message to display to the user on success
        # or raise a CommandError as a failure condition
        if options['myoption'] == 'default':
            return 'Success!'

        raise CommandError('Only the default is supported')

